package com.example.nick.bodyapplication.widget

import android.animation.ValueAnimator
import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View

/**
 * 数字滚动实现，思路 可以drawtext 移动Y坐标，也可以移动画布canvas.translate
 * @property paintNumber Paint
 * @property paintLine Paint
 * @property moveY Float
 * @property floatAnimator (android.animation.ValueAnimator..android.animation.ValueAnimator?)
 */
class NumberView : View {
    var paintNumber = Paint(Paint.ANTI_ALIAS_FLAG)
    var paintLine = Paint(Paint.ANTI_ALIAS_FLAG)

    var moveY = 0f

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {
        paintNumber.color = Color.BLUE
        paintNumber.strokeWidth = 5f
        paintNumber.textSize = dp2px(24f).toFloat()
        paintNumber.textAlign = Paint.Align.CENTER
        floatAnimator.duration = 2000
        floatAnimator.repeatCount = -1
        floatAnimator.addUpdateListener {
            moveY = it.animatedValue as Float
            Log.i("NumberView", "moveY:${moveY}")
            invalidate()
        }
        floatAnimator.start()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas) {
        canvas.save()
        canvas.translate(0f, moveChange())
        drawText(canvas)
        canvas.restore()
    }

    fun drawText(canvas: Canvas) {
        val bgRect = Paint()
        bgRect.style = Paint.Style.FILL
        bgRect.color = Color.TRANSPARENT
        val rectF = RectF(
            0f,
            0f,
            width.toFloat(),
            height.toFloat()
        )
        canvas.drawRect(rectF, bgRect)
        //计算baseline
        val fontMetrics = paintNumber.fontMetrics
        val distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        val baseline = rectF.centerY() + distance
        Log.d(
            "NumberView",
            "fontMetrics top:${fontMetrics.top}, ascent:${fontMetrics.ascent}, descent:${fontMetrics.descent}, bottom:${fontMetrics.bottom}"
        )
        canvas.drawText(
            "100",
            rectF.centerX(),
            baseline + moveChange(),
            paintNumber
        )

        paintLine.strokeWidth = 4f
        paintLine.color = Color.RED;
        canvas.drawLine(0f, height / 2f, width.toFloat(), height / 2f, paintLine)
        paintLine.color = Color.BLUE;
        canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), paintLine)
        Log.i("NumberView", "draw moveY:${moveY}")
    }

    fun dp2px(dpValue: Float): Int {
        return (0.5f + dpValue * Resources.getSystem().displayMetrics.density).toInt()
    }

    var floatAnimator = ValueAnimator.ofFloat(0f, 1f)

    fun moveChange(): Float {
        return if (moveY < 0.5) {
            moveY * height
        } else {
            (moveY - 1) * height
        }
    }
}